package cn.com.fuhuitong.main.home.adapter

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.TextSwitcher
import android.widget.TextView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.custom.AnimationTextSwitcher
import cn.com.fuhuitong.main.home.entity.BannerEntity
import cn.com.fuhuitong.main.home.entity.ExpressNewEntity
import cn.com.fuhuitong.main.home.entity.GoodsListEntity
import cn.com.fuhuitong.main.home.entity.HomeIconEntity
import cn.com.fuhuitong.utils.SpanUtils
import cn.com.fuhuitong.utils.WebViewUtils
import com.alibaba.android.vlayout.LayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.RectangleIndicator
import kotlinx.android.synthetic.main.item_home_action_image.view.*
import kotlinx.android.synthetic.main.item_home_banner.view.*
import kotlinx.android.synthetic.main.item_home_bulletin.view.*
import kotlinx.android.synthetic.main.item_home_category.view.*
import kotlinx.android.synthetic.main.item_home_goods.view.*
import kotlinx.android.synthetic.main.item_home_search.view.*


enum class HVT(value: Int) {
    HOME_SEARCH(1),
    HOME_BANNER(2),
    HOME_CATEGORY(3),
    HOME_BULLETIN(4),
    HOME_ACTION_IMAGE(5),
    HOME_GOODS(6),
    HOME_TITLE(7)
}

/**
 *首页头部搜索适配器
 * */
class HomeSearchAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_search,
    mItemViewType = HVT.HOME_SEARCH.ordinal
) {
    init {
        data.add("搜索")
        addChildClickViewIds(R.id.frame_home_search)
    }

    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        itemView.text_home_search.text = data[position]
    }
}

/**
 *首页轮播图适配器
 * */
class HomeBannerAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<MutableList<BannerEntity>>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_banner,
    mItemViewType = HVT.HOME_BANNER.ordinal
) {

    private var onBannerListener: ((View, Int, BannerEntity) -> Unit)? = null

    fun setOnBannerListener(onBannerListener: ((View, Int, BannerEntity) -> Unit)) {
        this.onBannerListener = onBannerListener
    }

    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        val bannerHeadsImage = itemView.banner_home_image
        bannerHeadsImage.setIndicator(RectangleIndicator(mContext))
            .setBannerRound(20f)
        val bannerImageAdapter = object : BannerImageAdapter<BannerEntity>(data[position]) {
            override fun onBindView(
                holder: BannerImageHolder, data1: BannerEntity, position1: Int, size: Int
            ) {
                Glide.with(holder.itemView).load(getImageUrl(data1.image))
                    .into(holder.imageView)
                setOnBannerListener { data, position ->
                    onBannerListener?.invoke(holder.imageView, position, data as BannerEntity)
                }
            }
        }
        bannerHeadsImage.adapter = bannerImageAdapter
    }
}

/**
 *首页分类适配器
 * */
class HomeCategoryAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<HomeIconEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_category,
    mItemViewType = HVT.HOME_CATEGORY.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        itemView.text_category_name.text = data[position].title
        Glide.with(itemView).load(getImageUrl(data[position].image))
            .into(itemView.image_category_icon)

    }
}

/**
 *首页快报通知适配器
 * */
class HomeBulletinAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<MutableList<ExpressNewEntity>>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_bulletin,
    mItemViewType = HVT.HOME_BULLETIN.ordinal
) {

    override fun onBindViewHolder(
        itemView: View,
        position: Int,
        itemData: MutableList<ExpressNewEntity>
    ) {
        super.onBindViewHolder(itemView, position, itemData)

        if (itemView.text_home_bulletin_title.childCount < 2) {
            itemView.text_home_bulletin_title.setFactory {
                val textView = TextView(mContext)
                textView.setTextColor(mContext.resources.getColor(R.color.colorMainGray))
                textView.textSize = 13F
                textView.isSingleLine = true
                textView.ellipsize = TextUtils.TruncateAt.END
                return@setFactory textView
            }
            itemView.text_home_bulletin_title.setOnClickListener {
                setOnItemChildClick(it, it.tag as Int, itemData)
            }
            val bulletins: MutableList<String> = mutableListOf()
            itemData.forEach {
                bulletins.add(it.title)
            }
            AnimationTextSwitcher(
                itemView.text_home_bulletin_title, bulletins
            ).create()
        }
    }
}

/**
 *首页活动图适配器
 * */
class HomeActionImageAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_action_image,
    mItemViewType = HVT.HOME_ACTION_IMAGE.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        Glide.with(itemView).load(getImageUrl(data[position]))
            .into(itemView.image_action_icon)
    }
}

/**
 *首页商品适配器
 * */
class HomeGoodsAdapter(
    private val mContext: Context, private var mHelper: LayoutHelper
) : BaseDelegateAdapter<GoodsListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_goods,
    mItemViewType = HVT.HOME_GOODS.ordinal
) {

    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)


        val goodsListEntity: GoodsListEntity = data[position]

        Glide.with(itemView).load(getImageUrl(goodsListEntity.image))
            .into(itemView.image_goods_icon)

        itemView.text_goods_name.text = goodsListEntity.title

        itemView.text_goods_price.text = SpanUtils.setMoneyStyle(goodsListEntity.price.toDouble())
    }
}


/**
 *首页标题适配器
 * */
class HomeTitleAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_home_title,
    mItemViewType = HVT.HOME_TITLE.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        itemView.text_home_title.text = data[position]
    }
}
